home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / conqsrc.lha / Conquest / src / movement.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-07  |  7.0 KB  |  313 lines

  1. /* Movement.c: Routines for moving fleets around */
  2. #include <stdio.h>
  3. #include "defs.h"
  4. #include "structs.h"
  5. #include "vars.h"
  6. #include "protos.h"
  7.  
  8. boolean lose(int *ships, int typ, float percent)
  9. {
  10.   int i,sleft;
  11.   boolean any_losses = FALSE;
  12.  
  13.   sleft = *ships;
  14.   for (i = 1; i<=*ships; i++) 
  15.   {
  16.     if (rnd(100)/100.0 > percent)
  17.     {
  18.       any_losses = TRUE;
  19.       sleft = sleft - 1;
  20.     }
  21.   }
  22.   if (sleft < *ships) 
  23.   {
  24.     printf(" %2d%c", *ships-sleft, typ);
  25.     *ships = sleft;
  26.   }
  27.  
  28.   return(any_losses);
  29. }
  30.  
  31. void fleet_explore(int fleet)
  32. {
  33.   int loss;
  34.   float prob;
  35.  
  36.   left_line[20] = true;
  37.   clear_left();
  38.   point(1,19);
  39.   printf("Task force %c exploring %c.\n", '`'+fleet, 
  40.      tf[PLAYER][fleet].dest + '@');
  41.  
  42.   if (tf[PLAYER][fleet].t != 0)
  43.     prob = (t_e_prob + rnd(t_e_var) * tf[PLAYER][fleet].t) / 100.0;
  44.   if (tf[PLAYER][fleet].s != 0)
  45.     prob = (s_e_prob + rnd(s_e_var) * tf[PLAYER][fleet].s) / 100.0;
  46.   if (tf[PLAYER][fleet].c != 0)
  47.     prob = (c_e_prob + rnd(c_e_var) * tf[PLAYER][fleet].c) / 100.0;
  48.   if (tf[PLAYER][fleet].b != 0)
  49.     prob = (b_e_prob + rnd(b_e_var) * tf[PLAYER][fleet].b) / 100.0;
  50.   if (prob > 100) prob=100;
  51.  
  52.   loss = FALSE;
  53.   loss |= lose(&tf[PLAYER][fleet].t, 't', prob);
  54.   loss |= lose(&tf[PLAYER][fleet].s, 's', prob);
  55.   loss |= lose(&tf[PLAYER][fleet].c, 'c', prob);
  56.   loss |= lose(&tf[PLAYER][fleet].b, 'b', prob);
  57.   if (!loss) printf("No ships");
  58.   printf(" destroyed.");
  59.  
  60.   tf[PLAYER][fleet].eta = 1;      /* fool zero tf */
  61.   zero_tf(PLAYER, fleet);
  62.   tf[PLAYER][fleet].eta = 0;      /* fool zero tf */
  63.  
  64.   left_line[23] = TRUE;
  65.  
  66.   if (tf[PLAYER][fleet].dest) /* Anything left? */
  67.   {
  68.     tf_stars[tf[PLAYER][fleet].dest][PLAYER]++;
  69.     board[tf[PLAYER][fleet].x][tf[PLAYER][fleet].y].enemy=' ';
  70.     update_board(tf[PLAYER][fleet].x,tf[PLAYER][fleet].y,left);
  71.     stars[tf[PLAYER][fleet].dest].visit[PLAYER]=TRUE;
  72.     point(50,1);
  73.     print_star(tf[PLAYER][fleet].dest);
  74.     clear_field();
  75.   }
  76.   pause();
  77. }
  78.  
  79. void move_ships()
  80. {
  81.   float ratio;
  82.   int there,dx,dy,i; 
  83.   tteam tm; 
  84.   struct stplanet *pplanet;
  85.   boolean any;
  86.   
  87.   /* clear the board */
  88.   for (i = 1; i<=MAX_FLEETS; i++)
  89.   {
  90.     if ((tf[player][i].dest != 0) && (tf[player][i].eta != 0))
  91.     {
  92.       board[tf[player][i].x][tf[player][i].y].tf = ' ';
  93.       update_board(tf[player][i].x,tf[player][i].y,right);
  94.     }
  95.   }
  96.  
  97.   /* move ships of both teams */
  98.   for (tm = ENEMY; tm < NONE; tm++)
  99.   {
  100.     for (i=1; i<=MAX_FLEETS; i++)
  101.     {
  102.       if ((tf[tm][i].dest != 0) && (tf[tm][i].eta != 0))
  103.       {
  104.     tf[tm][i].eta--;
  105.  
  106.     if (tm==player)
  107.     {
  108.       dx = stars[tf[tm][i].dest].x;
  109.       dy = stars[tf[tm][i].dest].y;
  110.       ratio = 1.0 - ((float)tf[tm][i].eta / tf[tm][i].origeta);
  111.       tf[tm][i].x = tf[tm][i].xf + round(ratio*(dx-tf[tm][i].xf));
  112.       tf[tm][i].y = tf[tm][i].yf + round(ratio*(dy-tf[tm][i].yf));
  113.       
  114.       if (tf[tm][i].eta == 0) /* Update for arrival */
  115.       {
  116.         if (!stars[tf[tm][i].dest].visit[tm])
  117.           fleet_explore(i);
  118.         else
  119.           /* Already knew this planet */
  120.           tf_stars[tf[tm][i].dest][tm]++;
  121.         if (tf[tm][i].dest != 0) /* Any survivors ? */
  122.         {
  123.           pplanet = stars[tf[tm][i].dest].first_planet;
  124.           while (pplanet != nil) 
  125.           {
  126.         pplanet->psee_capacity = pplanet->capacity;
  127.         pplanet = pplanet->next;
  128.           }
  129.           player_arrivals[tf[tm][i].dest]=true;
  130.         }
  131.       }
  132.     }
  133.     if ((tm==ENEMY) && (tf[tm][i].eta==0))
  134.     {
  135.       pplanet=stars[tf[tm][i].dest].first_planet;
  136.       stars[tf[tm][i].dest].visit[ENEMY]=true;
  137.       while (pplanet!=nil)
  138.       {
  139.         pplanet->esee_team = pplanet->team;
  140.         pplanet=pplanet->next;
  141.       }
  142.       if (tf_stars[tf[tm][i].dest][ENEMY]>0)
  143.       {
  144.         for (there = 1; (tf[ENEMY][there].dest != tf[ENEMY][i].dest) || 
  145.          (tf[ENEMY][there].eta != 0) || (there == i); there++);
  146.         joinsilent(ENEMY,&tf[ENEMY][i],&tf[ENEMY][there]);
  147.       }
  148.       if ((tf_stars[tf[tm][i].dest][player] > 0) ||
  149.           (col_stars[tf[tm][i].dest][player] > 0))
  150.         enemy_arrivals[tf[tm][i].dest]=true;
  151.       tf_stars[tf[tm][i].dest][tm]++;
  152.     }
  153.       }
  154.     }
  155.   } 
  156.  
  157.   /* put the good guys on the board */
  158.   for (i=1; i<=MAX_FLEETS; i++)
  159.   {
  160.     if (tf[player][i].dest != 0)
  161.     {
  162.       tf[player][i].blasting = false;
  163.       dx = tf[player][i].x;
  164.       dy = tf[player][i].y;
  165.       if (board[dx][dy].tf == ' ')
  166.     board[dx][dy].tf = i+'a'-1;
  167.       else if (board[dx][dy].tf != i+'a'-1)
  168.     board[dx][dy].tf='*';
  169.       update_board(dx,dy,right);
  170.     }
  171.   }
  172.   any = false;
  173.   for (i = 1; i<=nstars; i++)
  174.   {
  175.     if (player_arrivals[i])
  176.     {
  177.       if (!any)
  178.       {
  179.     point(33,21);
  180.     printf("Player arrivals :               ");
  181.     point(50,21);
  182.     any = true;
  183.       }
  184.       putchar(i+'A'-1);
  185.       player_arrivals[i]=false;
  186.     }
  187.   }
  188.   if ((!any) && (terminal_type != hardcopy))
  189.   {
  190.     point(33,21);
  191.     printf(blank_line);
  192.   }
  193.  
  194.   any = false;
  195.   for (i = 1; i<=nstars; i++)
  196.   {
  197.     if (enemy_arrivals[i])
  198.     {
  199.       if (!any)
  200.       {
  201.     point(33,22);
  202.     printf("Enemy arrivals  :               ");
  203.     point(50,22);
  204.     any = true;
  205.       }
  206.       putchar(i+'A'-1);
  207.       enemy_arrivals[i]=false;
  208.     }
  209.   }
  210.   if ((! any) && (terminal_type != hardcopy))
  211.   {
  212.     point(33,22);
  213.     printf(blank_line);
  214.   }
  215.  
  216.   any = false;
  217.   for (i = 1; i<=nstars; i++)
  218.   {
  219.     if (en_departures[i])
  220.     {
  221.       if (!any)
  222.       {
  223.     point(33,23);
  224.     printf("Enemy departures:               ");
  225.     point(50,23);
  226.     any = true;
  227.       }
  228.       putchar(i+'A'-1);
  229.       en_departures[i]=false;
  230.     }
  231.   }
  232.   if ((!any) && (terminal_type != hardcopy))
  233.   {
  234.     point(33,23);
  235.     printf(blank_line);
  236.   }
  237.  
  238.   for (i = 1; i<=nstars; i++)
  239.     revolt(i);
  240. }
  241.  
  242. boolean set_des(int tf_num)
  243. {
  244.   int st_num, min_eta, dst, from_star; 
  245.   char istar; 
  246.   float r;
  247.  
  248.   if (tf[player][tf_num].eta != 0)
  249.   { /* Cancelling previous orders */
  250.     tf[player][tf_num].eta = 0;
  251.  
  252.     from_star = board[tf[player][tf_num].x][tf[player][tf_num].y].star-'A'+1;
  253.     tf[player][tf_num].dest = from_star;
  254.     tf_stars[from_star][player]++;
  255.  
  256.     printf("(Cancelling previous orders)");
  257.     point(1,y_cursor + 1);
  258.   }
  259.  
  260.   printf(" to star:");
  261.   point(10,y_cursor);
  262.  
  263.   istar = get_char();
  264.   st_num = istar-'A'+1;
  265.  
  266.   if ((st_num < 0) || (st_num > NUM_STARS))
  267.   {
  268.     error("  !illegal star");
  269.     return(TRUE);
  270.   }
  271.  
  272.   r = dist(st_num, tf[1][tf_num].dest);
  273. /*
  274.   r=sqrt((float) (((stars[st_num].x-tf[1][tf_num].x)*
  275.            (stars[st_num].x-tf[1][tf_num].x)) +
  276.           ((stars[st_num].y-tf[1][tf_num].y) * 
  277.            (stars[st_num].y-tf[1][tf_num].y))));
  278. */
  279.   point(1,y_cursor + 1);
  280.   printf("   distance:%5.1f", r);
  281.   dst = r - 0.049 + 1;
  282.   
  283.   /* Only scout can fly so far */
  284.   if ((dst > range[player]) &&
  285.       ((tf[1][tf_num].b != 0) ||
  286.        (tf[1][tf_num].c != 0) ||
  287.        (tf[1][tf_num].t != 0))) 
  288.   {
  289.     error("  !maximum range is %2d", range[player]);
  290.     return(TRUE);
  291.   }
  292.   
  293.   if (r < 0.5)
  294.   {
  295.     point(1, y_cursor + 1);
  296.     printf("Tf remains at star");
  297.     return(TRUE);
  298.   }
  299.  
  300.   min_eta = ((dst-1) / vel[player]) + 1;
  301.   point(1,y_cursor + 1);
  302.   printf("eta in %2d turns", min_eta);
  303.  
  304.   tf_stars[tf[player][tf_num].dest][player]--;
  305.   tf[player][tf_num].dest=st_num;
  306.   tf[player][tf_num].eta = min_eta;
  307.   tf[player][tf_num].origeta= tf[player][tf_num].eta;
  308.   tf[player][tf_num].xf= tf[player][tf_num].x;
  309.   tf[player][tf_num].yf=tf[player][tf_num].y;
  310.  
  311.   return(FALSE);
  312. }
  313.